package com.atguigu.yygh.user.service.impl;

import com.atguigu.yygh.cmn.client.DictFeignClient;
import com.atguigu.yygh.model.user.Patient;
import com.atguigu.yygh.user.mapper.PatientMapper;
import com.atguigu.yygh.user.service.PatientService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 就诊人表 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2023-03-27
 */
@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements PatientService {

    //在远程调用的地方直接注入
    @Autowired
    private DictFeignClient dictFeignClient;

    @Override
    public List<Patient> getPatientlist(QueryWrapper<Patient> queryWrapper) {
        //根据id查出所用户下的就诊人
        List<Patient> patients = baseMapper.selectList(queryWrapper);
        patients.stream().forEach(patient -> {
            //转成对应的文字将certificatesType
            this.packagePatient(patient);

        });
        return patients;
    }

    @Override
    public Patient edit(Long id) {
        //根据就诊人id查询就诊人信息
        Patient patient = baseMapper.selectById(id);
        this.packagePatient(patient);
        return patient;
    }

    //将公共的代码提取到一个方法中
    public void packagePatient(Patient patient) {
        String certificatesType = patient.getCertificatesType();
        String nameByValue = dictFeignClient.getNameByValue(Long.parseLong(certificatesType));
        //将对应的编号转换成对应的文字信息,使用OpenFeign远程调用
        patient.getParam().put("certificatesTypeString",nameByValue); //证件类型
        String provinceString = dictFeignClient.getNameByValue(Long.parseLong(patient.getProvinceCode()));
        patient.getParam().put("provinceString",provinceString); //省编号转成文字
        //市编号转成文字
        String cityString = dictFeignClient.getNameByValue(Long.parseLong(patient.getCityCode()));
        patient.getParam().put("cityString",cityString);
        //区编号转成文字
        String districtString = dictFeignClient.getNameByValue(Long.parseLong(patient.getDistrictCode()));
        patient.getParam().put("districtString",districtString);
        //详细地址拼接成文字
        patient.getParam().put("fullAddress",provinceString+cityString+districtString + patient.getAddress());
    }
}
